package com.atguigu.guli.service.cms.service.impl;

import com.atguigu.guli.service.cms.entity.Ad;
import com.atguigu.guli.service.cms.entity.AdType;
import com.atguigu.guli.service.cms.mapper.AdMapper;
import com.atguigu.guli.service.cms.service.AdService;
import com.atguigu.guli.service.cms.service.AdTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 广告推荐 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-08-07
 */
@Service
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad> implements AdService {

    @Autowired
    AdTypeService adTypeService;

    /*
    查询首页广告
     */
    @Cacheable(key = "'ads'",value = "ad")
    @Override
    public Map<String, List<Ad>> getAds() {
        //查询广告分类信息
        List<AdType> list = adTypeService.list();
        //map->key=广告分类名称 value=分类下的内容
        Map<String, List<Ad>> type_id1 = list.stream().collect(Collectors.toMap(k ->  k.getTitle(), v -> {
            List<Ad> type_id = this.baseMapper.selectList(new QueryWrapper<Ad>().eq("type_id", v.getId()));
            return type_id;
        }));
        return type_id1;
    }
}
